from models import db
from models.user import User
from models.admin import  Admin
import hashlib,random

def generate_token(password):
    iv = random.randint(1000,9999)
    data = password+str(iv)
    token = hashlib.new('md5', data.encode('utf-8')).hexdigest()
    return token

def verify_admin(username,password):
    result = db.session.query(Admin).filter_by(username=username).first()
    try:
        if result.password == password:
            return result
        else:
            return False
    except Exception as e:
        print(e)

def verify_user(phone,password):
    result = db.session.query(User).filter_by(phone=phone).first()
    try:
        if result.password == password:
            return  result
        else:
            return False
    except Exception as e:
        print(e)


if __name__ == "__main__":
    a = generate_token("admin")
    print(a)